<template>
  <div class="advert-space">
    <el-card>
      <div slot="header">
        <el-button @click="$router.push({
          name: 'advert-space-create'
        })">添加广告位</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="advertSpaces"
        style="width: 100%">
        <el-table-column
          prop="spaceKey"
          label="spaceKey"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="广告位名称"
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
        >
          <template slot-scope="scope">
            <span>{{scope.row.createTime | formatDate}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="updateTime"
          label="更新时间"
        >
          <template slot-scope="scope">
            <span>{{scope.row.updateTime | formatDate}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="$router.push({
                name: 'advert-space-edit',
                params: {
                  advertId: scope.row.id
                }
              })"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-wrapper">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="pageNum"
          :disabled="loading"
          :page-sizes="[100, 150, 200]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAllSpaces } from '@/services/advert'

export default Vue.extend({
  name: 'AdvertSpaceIndex',
  data () {
    return {
      loading: false,
      advertSpaces: [],
      pageSize: 100,
      pageNum: 1,
      total: 0
    }
  },
  created () {
    this.loadAderts()
  },
  methods: {
    async loadAderts () {
      this.loading = true
      const { data } = await getAllSpaces(this.pageSize, this.pageNum)
      this.advertSpaces = data.content
      this.loading = false
      this.total = data.content.length
    },
    handleSizeChange (val: number) {
      this.pageSize = val
      this.pageNum = 1
      this.loadAderts()
    },
    handleCurrentChange (val: number) {
      this.pageNum = val
      this.loadAderts()
    }
  }
})
</script>

<style lang="scss" scoped>
.pagination-wrapper {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
</style>
